<!--
 * @Desc:
 * @author cheegon
 * @date 2024/07/02  下午3:49
-->
<template>
  <div>
    <!--    面包屑导航区-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/projects' }">项目管理</el-breadcrumb-item>
      <el-breadcrumb-item>项目发布</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="hover">
      <el-row>
        <el-col :span="10">
          <el-form label-position="left" label-width="80px" style="text-align: left" ref="formRef"
                   :model="formData">
            <el-form-item label="项目名称" prop="title">
              <el-input v-model="formData.title"></el-input>
            </el-form-item>
            <el-form-item label="项目类型" prop="type">
              <el-select v-model="formData.type">
                <el-option v-for="item in types"
                           :value="item.id"
                           :label="item.name"
                           :key="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目描述" prop="content">
              <el-input v-model="formData.content" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="项目封面">
              <SimpleUpload :info="imageInfo" ref="uploadRef" @complete="handleUploadComplete"></SimpleUpload>
            </el-form-item>
            <el-form-item label="项目图片">
              <SimpleUpload :info="imageInfo" ref="uploadRef" @complete="handleUploadComplete"></SimpleUpload>
            </el-form-item>
            <el-form-item label="技术栈" prop="techs">
              <el-input v-model="formData.techs" placeholder="请输入技术栈,以英文逗号分割"></el-input>
            </el-form-item>
            <el-form-item label="项目地址" prop="url">
              <el-input v-model="formData.url" placeholder="请输入项目地址"></el-input>
            </el-form-item>
            <el-form-item style="text-align: center">
              <el-button type="primary" @click="handleSubmit" v-if="isEdit">编辑</el-button>
              <el-button type="primary" @click="handleSubmit" v-else>发布</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import SimpleUpload from '@/components/SimpleUpload.vue'
import { reqAddProject, reqEditProject, reqProjectDetail } from '@/api/adminApi'

export default {
  name: "AEditProject",
  components: { SimpleUpload },
  props: [],
  data() {
    return {
      id: 0,
      isEdit: false,
      formData: {
        title: '',
        type: 0,
        content: '',
        techs: '',
        url: '',
        picUrl: ''
      },
      formRules: {
        title: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '项目地址不能为空', trigger: 'blur' }
        ],
      },
      imageInfo: {},
      types: [
        { id: 0, name: '完整项目' },
        { id: 1, name: '小练习' }
      ]
    }
  },
  created() {
    if (this.$route.path == '/admin/project/edit') {
      this.isEdit = true
      this.id = this.$route.query.id
      this.formData.id = this.id
      this.getInfo()
    }
  },
  methods: {
    getInfo() {
      reqProjectDetail(this.id).then(data => {
        this.formData = data
        this.imageInfo.imageList = [data.picUrl]
        this.$refs.uploadRef.refresh()
      })
    },
    handleUploadComplete(value) {
      if (value && value.length) {
        this.formData.picUrl = value[0]
      } else {
        this.formData.picUrl = ''
      }
    },
    handleSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return;
        if (this.isEdit) {
          this.handleEdit()
        } else {
          this.handleAdd()
        }
      })
    },
    // 发布项目
    handleAdd() {
      reqAddProject(this.formData).then(data => {
        this.$message.success('发布成功！')
        this.$router.back()
      })
    },
    // 更新项目
    handleEdit() {
      reqEditProject(this.formData).then(data => {
        this.$message.success('更新成功！')
        this.$router.back()
      })
    },
  }
}
</script>

<style lang="stylus" scoped>

</style>
